package com.example.myshop.service.Imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.myshop.domain.UserPurseDao;
import com.example.myshop.entity.UserPurse;
import com.example.myshop.service.UserPurseService;
import com.example.myshop.service.ex.InsertException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserPurseServiceImp extends ServiceImpl<UserPurseDao, UserPurse> implements UserPurseService {
    @Autowired
    UserPurseDao userPurseDao;



    @Override
    public void insert(UserPurse user) {
        user.setModifiedUser(user.getUsername());
        user.setModifiedTime(new Date());
        Integer a= user.getAddAmount();
        Integer b= user.getPurse();
        user.setPurse(a+b);
        Integer rows=userPurseDao.insert(user);
        if(rows!=1){
            throw new InsertException("用户充值记录录入产生未知的异常");
        }
    }

    @Override
    public void inserts(UserPurse user) {
        user.setModifiedUser(user.getUsername());
        user.setModifiedTime(new Date());
        Integer a= user.getSubAmount();
        Integer b= user.getPurse();
        user.setPurse(b-a);
        Integer rows=userPurseDao.insert(user);
        if(rows!=1){
            throw new InsertException("用户充值记录录入产生未知的异常");
        }
    }

    @Override
    public List<UserPurse> getByUid(Long uid) {
        QueryWrapper<UserPurse>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("uid",uid);
        List<UserPurse>list= userPurseDao.selectList(queryWrapper);
        return list;
    }
}
